import {_decorator, Button, Component, director, instantiate, Label, Node, Prefab} from 'cc';
import Long from "long";
import {configsHelper} from "db://assets/script/configs-helper";
import {GameEvent} from "db://assets/script/game-event";
import {FishMoveEvent} from "db://assets/script/common/events/fish-events/fish-move-event";
import {showAlert} from "db://assets/script/game/alert/alert";
import {lobby} from "db://assets/script/lobby";
import {userInfo} from "db://assets/script/user-info";
import {RankType} from "db://assets/proto/new-world-api/v1/user";
import CommonRanks from "db://assets/script/game/mainui/ranks/common-ranks";
import {PropItem} from "db://assets/script/game/mainui/props/prop-item";
import {TalentHeadComp} from "db://assets/script/game/mainui/talent-head-comp";
import {TalentHeadCompWithName} from "db://assets/script/game/mainui/talent-head-camp-with-name";
import {fishConfigsLoader} from "db://assets/script/configs/fish-configs-loader";

const {ccclass, property} = _decorator;

@ccclass('ZoneInfo')
export class ZoneInfo extends Component {

    @property(Label)
    title!: Label

    @property(Label)
    zoneLevel!: Label

    @property(Node)
    fishInZoom!: Node

    @property(Node)
    explorerOutput!: Node

    @property(Button)
    gotoBtn!: Button

    @property(Button)
    poolRankBtn!: Button

    @property(Prefab)
    propItemPrefab!: Prefab

    @property(Prefab)
    talentHeadPrefab!: Prefab

    init(zoneId: Long, willMoveFishId: Long | null = null) {
        this.explorerOutput.destroyAllChildren()
        this.fishInZoom.destroyAllChildren()
        const zoneConfig = configsHelper.loadZoneConfig(zoneId)
        if (zoneConfig) {
            this.title.string = zoneConfig!.zoneName
            const lc = fishConfigsLoader.findFishLevelConfigsByLevel(zoneConfig!.wildLevelEnd)
            this.zoneLevel.string = lc?.name || ''
        }
        if (willMoveFishId) {
            const ff = userInfo.fishInfo.loadFishById(willMoveFishId)
            if (ff?.zoneId.neq(zoneId)) {
                this.gotoBtn.node.active = true
                this.gotoBtn.node.on(Button.EventType.CLICK, () =>
                    showAlert('移动', '是否移动到「' + zoneConfig!.zoneName + '」?', () => {
                            director.emit(GameEvent.fishActionMove, new FishMoveEvent(willMoveFishId!, zoneId))
                            lobby.instance.clearAllTempUI()
                        }
                    )
                )
            }
        }
        for (let reward of zoneConfig!.outputRewards) {
            const pn = instantiate(this.propItemPrefab)
            const propItem = pn.getComponent(PropItem)!
            let desc = '少许'
            if (reward.num.gt(10)) {
                desc = '海量'
            } else if (reward.num.gt(5)) {
                desc = '许多'
            }
            propItem.initWithDesc(reward.propId.toNumber(), desc)
            this.explorerOutput.addChild(pn)
        }
        for (let fish of userInfo.fishInfo.loadFishes().values()) {
            if (fish.freshFish.zoneId.neq(zoneId)) continue
            const hn = instantiate(this.talentHeadPrefab)
            const talentHead = hn.getComponent(TalentHeadCompWithName)!
            talentHead.setHead(fish.freshFish.id, fish.freshFish.baseInfo!.gender, fish.freshFish.baseInfo!.talent, fish.freshFish.masterInfo!.masterId, fish.freshFish.baseInfo!.fishName)
            this.fishInZoom.addChild(hn)
        }
        this.poolRankBtn.node.on(Button.EventType.CLICK, () => {
            const poolRankNode = lobby.instance.addUIWithClickMask('prefab/ranks/common-ranks', 0, (node) => node.destroy())
            const poolRank = poolRankNode.getComponent(CommonRanks)!
            poolRank.show()
            poolRank.loadRank(RankType.level, zoneId)
        })
    }


}